<?php
class classItemDetail extends phpajax {
	private $_Follow;
	
	function __construct(){
		$this->_Follow = new classFollow();
	}
	
	//-----------------------------------------------------
	//AJAX call - inherit from phpajax, reading input values
	function input(){
		aread("Show_Product_Detail_By_ID");
        aread("ProductType");
	}
	
	//-----------------------------------------------------
	//AJAX call - inherit from phpajax
	function loading() { 
		
	} 
	
	//-----------------------------------------------------
	//AJAX call - inherit from phpajax
	function main() {
		$iItemID = & $this->Show_Product_Detail_By_ID;
        $container = 'flow_container2';
        $childWnd = false;
        if(trim($this->ProductType) != '') {$container='flow_container'; $childWnd=true;};

		if(is_numeric($iItemID) and ($iItemID>0)){
            $_Wnd = new classWindow();
            aprint($container,$_Wnd->ShowByAJAX($container,$this->getAjaxProductDetail($iItemID),$childWnd));
        }
		else
			aprint($container,'');
	}

	
	//for phpajax
	private function getToBasketLink($iItemID, $ProductType=''){
		//hidden variables for data moving are defined in classProduct->GetProductsList() function!!!!!
        $result = '';
        $toBasketValue = '1';

        if(_USE_INPUT_FOR_DEFINING_COUNT_TO_PUT_TO_BASKET_=='YES'){
            $_PSRVC = new classProductService();
            $PDetail= $_PSRVC->getProductInfo($iItemID);
            $result = '<input type="text" name="'.$ProductType.'ProductToBasket_CountInput" id="'.$ProductType.'ProductToBasket_CountInput" value="1" style="width:40px;"> ';
            $result.= $PDetail['Product_UnitType'];
            $result.= '<br>';
            $toBasketValue = 'document.getElementById(\''.$ProductType.'ProductToBasket_CountInput\').value';
        }
		$result.= '
			<a
				href="'._URL_INACTIVE_ADDR_.'"
				class="css_product_to basket"
				'.$this->_Follow->getJS(_i18n_product_INSERT_THIS_PRODUCT_INTO_BASKET_).'
				onmousedown="
					document.getElementById(\'ProductToBasket_ID\').value='.$iItemID.';
					document.getElementById(\'ProductToBasket_Count\').value='.$toBasketValue.';
					document.getElementById(\'ProductToBasket_Details\').value=-1;
                    document.getElementById(\'ProductType\').value = \''.$ProductType.'\';
				"					
				onmouseup="classBasket();"
			>
				'._i18n_product_TO_BASKET_.'
			</a>
		';
        return $result;
	}

    private function getResultMessageContainer($ProductType=''){
        return '<span id="'.$ProductType.'product_insert_into_basket_result_container"></span>';
    }
	
	private function getAjaxProductDetail($iItemID){
        if(trim($iItemID)=='' or !is_numeric($iItemID) or $iItemID<1) return '';

		$SQL_Result = mysql_query("SELECT * FROM products WHERE ID=".$iItemID);
		$sql_array_product = mysql_fetch_object($SQL_Result);
        $_Price = new classPriceExport();
		
		//make image's form
		$product_detail_image = $this->getImage($iItemID);
		//make others data
		$product_detail_name 	= $sql_array_product->Product_Name;        
		$product_detail_price 	= $_Price->simplePrice($iItemID,1);
		$product_detail_desc 	= $sql_array_product->Product_Descr;
        $product_to_basket 		= $this->getToBasketLink($iItemID, $this->ProductType);
        $product_result_msg     = $this->getResultMessageContainer($this->ProductType);
        if(_PRODUCT_CODE_SHOWING_=='YES') $product_detail_code 	= '['.$sql_array_product->Product_Code.']';

		//product form from template
		$_Skin = new classTemplate();
		$result = '';
		include $_Skin->getActual().'/product_detail.inc.php';
		
		return $result;
	}
	
	public function getImage($imgID, $isSharedProduct=false){
		if(
			file_exists(_PRODUCTS_IMAGE_DIR_.'/'.$imgID._IMAGES_FILE_TYPE_) and
			file_exists(_PRODUCTS_IMAGE_DIR_.'/'.$imgID._PREVIEW_IMAGE_SUFFIX_._IMAGES_FILE_TYPE_)
		){
            $_pService  = new classProductService;
            $asPInfo    = $_pService->getProductInfo($imgID);
            if($isSharedProduct) $cssClass='css_shared_product_preview_image'; else $cssClass='css_product_detail_preview_image';
			$result ='
				<a href="'._URL_INACTIVE_ADDR_.'"
                    '.$this->_Follow->getJS(_i18n_product_ENLARGE_IMAGE_BY_CLICK_).'
                    onmousedown="
                        document.getElementById(\'Show_Product_Image\').value=\''._PRODUCTS_IMAGE_DIR_.'/'.$imgID._IMAGES_FILE_TYPE_.'\';
                        document.getElementById(\'Image_Show_As_Child\').value=\'1\';
                        document.getElementById(\'Show_Product_ID\').value=\''.$imgID.'\';
                    "
                    onmouseup="classImageViewer();"
				>
                    <img
                        class="'.$cssClass.'"
                        src="'._PRODUCTS_IMAGE_DIR_.'/'.$imgID._PREVIEW_IMAGE_SUFFIX_._IMAGES_FILE_TYPE_.'"
                        alt="'.$asPInfo['Product_Name'].'"
                    >
				</a>
			';
        }
		else
			$result ='';	
		return $result;
	}

	//..........................................................................//
	//																			//
	//---------------NO AJAX, PROCESSING INPUT VALUES FROM URL------------------//
	//																			//
	//..........................................................................//
	
	
	//----------------------------------------------------
	//not for phpajax - data from URL
	public function getProductDetail($iProductID){	
	
		$SQL_Result = mysql_query("SELECT * FROM products WHERE ID=".$iProductID);
		$sql_array_product = mysql_fetch_object($SQL_Result);
        $_Price = new classPriceExport();
        $_SharedProduct = new classSharedProduct();
		
		//make image's form
		$product_detail_image   = $this->getImage($iProductID);
		//make others data
		$product_detail_name 	= $sql_array_product->Product_Name;        
		$product_detail_price 	= $_Price->simplePrice($iProductID,1);//$sql_array_product->Product_Price.' '._CURRENCY1_;
		$product_detail_desc 	= $sql_array_product->Product_Descr;
		$product_to_basket 		= $this->getToBasketLink($iProductID);
        $shared_products_list   = $_SharedProduct->getSharedProductsList($iProductID);
        $product_result_msg     = $this->getResultMessageContainer();
        if(_PRODUCT_CODE_SHOWING_=='YES') $product_detail_code = '['.$sql_array_product->Product_Code.']';
		
		//product form from template
		$_Skin = new classTemplate();
		$result = '';
		include $_Skin->getActual().'/product_detail.inc.php';
		
		return $result;
	}
}
?>